package com.treasure.mall.zbqmallmanager.controller.marketing;

import com.treasure.mall.biz.base.UserDetails;
import com.treasure.mall.biz.dto.condition.RedPacketCondition;
import com.treasure.mall.biz.dto.marketing.RedPacketSaveDTO;
import com.treasure.mall.biz.entity.marketing.RedPacketGrabLogPO;
import com.treasure.mall.biz.entity.marketing.RedPacketPO;
import com.treasure.mall.biz.service.core.marketing.RedPacketService;
import com.treasure.mall.biz.service.core.user.MerchantService;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.constant.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/8/3
 */
@Controller
@RequestMapping("/marketing/redPacket")
public class RedPacketController {
    @Autowired
    private RedPacketService redPacketService;
    @Autowired
    private MerchantService merchantService;

    @GetMapping("/list")
    public String list(
            @ModelAttribute("condition") RedPacketCondition condition,
            @RequestParam(required = false, defaultValue = "1") int pageIndex,
            Model model
    ) {
        Page<RedPacketPO> pos = redPacketService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, condition);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", pos.getTotalElements());
        model.addAttribute("totalPages", pos.getTotalPages());
        model.addAttribute("list", pos.getContent());

        return "marketing/red_packet_list";
    }

    @GetMapping("/edit")
    @PreAuthorize("hasRole('MERCHANT')")
    public String edit(@AuthenticationPrincipal UserDetails userDetails, Model model) {
        model.addAttribute("balance", merchantService.findById(userDetails.getId()).getUser().getBalance());

        return "marketing/red_packet_edit";
    }

    @PostMapping("/edit")
    @PreAuthorize("hasRole('MERCHANT')")
    @ResponseBody
    public ApiResult edit(RedPacketSaveDTO saveDTO, @AuthenticationPrincipal UserDetails userDetails) {
        saveDTO.setMerchantId(userDetails.getId());

        redPacketService.save(saveDTO);

        return ApiResult.ok();
    }

    @GetMapping("/grabLogs")
    public String grabLogs(Long redPacketId, Model model) {
        List<RedPacketGrabLogPO> pos = redPacketService.getGrabLogs(redPacketId);

        model.addAttribute("list", pos);

        return "marketing/red_packet_grab_log";
    }

    @PostMapping("/enableToggle")
    @PreAuthorize("hasRole('MERCHANT')")
    @ResponseBody
    public ApiResult enableToggle(Long redPacketId) {
        redPacketService.enabledToggle(redPacketId);

        return ApiResult.ok();
    }

    @GetMapping("/remainAmount")
    @ResponseBody
    public ApiResult remainAmount(Long redPacketId) {
        RedPacketPO po = redPacketService.findById(redPacketId);

        return ApiResult.ok(po.remainAmount());
    }

    @PostMapping("/recycle")
    @ResponseBody
    @PreAuthorize("hasRole('MERCHANT')")
    public ApiResult recycle(Long redPacketId) {
        redPacketService.recycle(redPacketId);

        return ApiResult.ok();
    }
}
